package org.rowan.adi.components.comp2d;

import java.util.LinkedList;
import org.rowan.adi.Vector;

/**
 * The LineSegment2D class represents a line segment in two dimensional space. A
 * line segment is comprised of two points, which represent the end points of the
 * line segment.
 *
 * @author Robert Russell
 * @version 1.0
 */
public class LineSegment2D extends Component2D {

    private Point2D point1; // the first endpoint of the line segment.
    private Point2D point2; // the second endpoint of the line segment.

    /**
     * Constructs an instance of LineSegment2D
     * @param point1 A 2D point representing an end point of the 2D line segment.
     * @param point2 A 2D point representing the other end point of the 2D line segment.
     * @throws IllegalArgumentException if both of the 2D points are the same.
     */
    public LineSegment2D(Point2D point1, Point2D point2) {
        if (point1.getX() == point2.getX() && point1.getY() == point2.getY()) {
            throw new IllegalArgumentException("Both points cannot be equal");
        }
        this.point1 = point1;
        this.point2 = point2;
        LinkedList<Point2D> points = new LinkedList<Point2D>();
        points.add(point1);
        points.add(point2);
        super.setListofPoints(points);
        super.setLocation(new Vector((point2.getX() + point1.getX()) / 2, (point2.getY() + point1.getY()) / 2));
        super.setTrajectory(Vector.ZERO_VECTOR);
    }

    /**
     * Gets the length of the LineSegment2D.
     * @return the length of the LineSegment2D.
     */
    public double getLength() {
        return Math.sqrt(Math.pow(Math.abs(point2.getX() - point1.getX()), 2) +
                Math.pow(Math.abs(point2.getY() - point1.getY()), 2));
    }
}
